package com.example.food_contract_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.food_contract_system.domain.Agency;
import com.example.food_contract_system.domain.Freeze;
import com.example.food_contract_system.domain.ShoppingCart;
import com.example.food_contract_system.mapper.AgencyMapper;
import com.example.food_contract_system.mapper.FreezeMapper;
import com.example.food_contract_system.service.ShoppingCartService;
import com.example.food_contract_system.mapper.ShoppingCartMapper;
import com.example.food_contract_system.util.R;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
* @author Lhy
* @description 针对表【shopping_cart】的数据库操作Service实现
* @createDate 2023-07-11 10:41:29
*/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService{

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private AgencyMapper agencyMapper;
    @Autowired
    private FreezeMapper freezeMapper;

    @Transactional
    @Override
    public R saveShoppingCart(String productList, String aId) {
        List<ShoppingCart> shoppingCarts = new ArrayList<>();

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(productList);
            JsonNode regionNode = jsonNode.get("productList");

            if (regionNode.isArray()) {
                Iterator<JsonNode> elements = regionNode.elements();
                while (elements.hasNext()){
                    JsonNode next = elements.next();
                    ShoppingCart shoppingCart = new ShoppingCart();

                    shoppingCart.setFId(next.get("fid").asInt());
                    shoppingCart.setFImg(next.get("fimg").asText());
                    shoppingCart.setFName(next.get("fname").asText());
                    shoppingCart.setFNum(next.get("fnum").asInt());

                    DecimalFormat df = new DecimalFormat("#.00");
                    String formattedNumber = df.format(next.get("fprice").asDouble());

                    shoppingCart.setFPrice(formattedNumber);
                    shoppingCart.setFType(next.get("ftype").asText());
                    shoppingCart.setDate(next.get("date").asText());

                    shoppingCart.setAId(aId);
                    shoppingCart.setSNum(next.get("snum").asInt());

                    shoppingCarts.add(shoppingCart);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Freeze freeze = freezeMapper.selectOne(new QueryWrapper<Freeze>()
                .eq("a_id", aId));
        if (freeze != null){
            return R.error().put("msg", "您的账号已冻结!");
        }

        if (shoppingCarts.size() > 0){
            Agency agency = agencyMapper.selectOne(new QueryWrapper<Agency>()
                    .eq("a_id", aId));
            if (agency != null) {

                if (agency.getAStatus().equals("0")) {
                    return R.error().put("msg", "您的账号还没通过审核，请等候");
                }
                if (agency.getAStatus().equals("2")) {
                    return R.error().put("msg", "您的账号未通过审核，请联系客服询问");
                }
                for (ShoppingCart shoppingCart : shoppingCarts) {
                    ShoppingCart fId = shoppingCartMapper.selectOne(new QueryWrapper<ShoppingCart>()
                            .eq("f_id", shoppingCart.getFId())
                            .eq("a_id", aId));
                    if (fId == null) {
                        UUID uuid = UUID.randomUUID();
                        String s = uuid.toString();
                        shoppingCart.setSId(s.substring(0, 4));

                        int insert = shoppingCartMapper.insert(shoppingCart);
                        if (insert == 0) {
                            return R.error();
                        }
                    } else {
                        int i = shoppingCartMapper.updateCart(shoppingCart, fId.getSId());
                        if (i == 0) {
                            return R.error();
                        }
                    }
                }
                return R.ok().put("msg","保存成功!");
            }else{
                return R.error().put("msg","请联系管理员");
            }
        }else{
            return R.error().put("msg","请联系管理员");
        }
    }

    @Override
    public R getShopping(String aId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(new QueryWrapper<ShoppingCart>()
                .eq("a_id",aId));
        return R.ok().put("data",shoppingCarts);
    }

    @Override
    public R deleteCart(String sId) {
        int id = shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>()
                .eq("s_id", sId));

        if (id > 0){
            return R.ok();
        }
        return R.error();
    }
}




